<?php
namespace WDB\Wrapper\User;
use WDB\Wrapper,
    WDB\WebUI,
    WDB\Exception,
    WDB,

    Nette,
    Nette\Image;


/**
 *
 * @author Richard Ejem <r.ejem9 gmail.com>
 */
class ImageColumn extends Wrapper\AbstractColumn
{
    /**@var string*/
    private $name;
    
    /**@var string*/
    private $title;
    
    /**@var string*/
    private $identifierColumn;
    /**@var \ImageTarget[]*/
    private $imageTargets;
    
    /**@var string*/
    private $defaultImage;
    
    /**@var int*/
    public $webUIDisplayMode;
    
    /**@var int*/
    public $imageResizeMode;
    
    /**
     * Column for storing images into filesystem
     *
     * @param \WDB\WebUI\Table $table
     * @param string $identifierColumn
     * @param \ImageTarget|ImageTarget[] $imageTargets
     */
    public function __construct($name, Wrapper\iTable $table, $identifierColumn, $imageTargets, $defaultImage = 0)
    {
        parent::__construct($table);
        if (!is_array($imageTargets))
        {
            $imageTargets = array(0=>$imageTargets);
        }
        $this->defaultImage = $defaultImage;
        
        $this->imageTargets = $imageTargets;
        
        $this->identifierColumn = $identifierColumn;
        $this->name = $name;
        $this->webUIDisplayMode = WebUI\Column::DISPLAY_EDIT | WebUI\Column::DISPLAY_DETAIL;
        
        $this->imageResizeMode = Nette\Image::FIT;
        
        $this->getOnSaved()->addListener(new WDB\Event\CallbackListener(array($this, 'saved')));
    }
    
    public function getTarget($name = NULL)
    {
        if ($name === NULL)
        {
            $name = $this->defaultImage;
        }
        if (isset($this->imageTargets[$name]))
        {
            return $this->imageTargets[$name];
        }
        else
        {
            throw new Exception\WDBException("Image target '$name' does not exist");
        }
    }
    
    public function getTargets()
    {
        return $this->imageTargets;
    }
    
    public function getName()
    {
        return $this->name;
    }
    
    public function getWebUIDisplayMode($original = NULL)
    {
        return $this->webUIDisplayMode;
    }
    
    public function isNullable()
    {
        return false;
    }
    
    public function saved(Wrapper\iRecord $record)
    {
        if (isset($_FILES[$this->name]))
        {
            $file = $_FILES[$this->name];
            if (is_uploaded_file($file['tmp_name']))
            {
                $this->saveImage($file['tmp_name'], $record);
            }
        }
    }
    
    protected function fetchPattern($pattern, Wrapper\iRecord $record)
    {
        return str_replace('*',
            $record[$this->identifierColumn],
            $pattern);
        
    }
    
    public function saveToRow(array &$row, $value)
    {
        //do nothing
        return;
    }
    
    public function getUrl(WDB\Wrapper\iRecord $record, $targetName = NULL)
    {
        return $this->fetchPattern($this->getTarget($targetName)->urlPattern, $record);
    }
    public function getFileName(WDB\Wrapper\iRecord $record, $targetName = NULL)
    {
        return $this->fetchPattern($this->getTarget($targetName)->filePattern, $record);
    }
    
    protected function saveImage($in, Wrapper\iRecord $record)
    {
        $image = Image::fromFile($in);
        foreach ($this->targets as $target)
        {
            $tgt = clone $image;
            $image->resize($target->width, $target->height, $this->imageResizeMode);
            $image->save($this->fetchPattern($target->filePattern, $record), 85, Image::JPEG);
        }        
    }
    
    public function getWebUIClass()
    {
        return 'ImageColumnWebUI';
    }
    
    public function getTitle()
    {
        return $this->title;
    }

    public function setTitle($title)
    {
        $this->title = $title;
    }


}
